<template>
  <dys-modal
    :open="open"
    :title="t('Menu Management')"
    @cancel="emit('cancel')"
    @ok="handleSubmit"
    :loading="loading"
    :width="800"
  >
    <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
      <a-row>
        <a-col :span="12">
          <a-form-item :label="t('菜单名称')" v-bind="validateInfos.menuName">
            <a-input
              v-model:value="modelRef.menuName"
              :placeholder="t('请输入菜单名称')"
              allow-clear
            ></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :label="t('菜单编码')" v-bind="validateInfos.menuCode">
            <a-input
              v-model:value="modelRef.menuCode"
              :placeholder="t('请输入菜单编码')"
              allow-clear
            ></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :label="t('菜单图标')" v-bind="validateInfos.menuIcon">
            <select-icon
              v-model:value="modelRef.menuIcon"
              :placeholder="t('请选择菜单图标')"
            ></select-icon>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :label="t('菜单状态')" v-bind="validateInfos.menuStatus">
            <a-switch
              v-model:checked="modelRef.menuStatus"
              checked-value="Y"
              un-checked-value="N"
              :checked-children="t('有效')"
              :un-checked-children="t('无效')"
              allow-clear
            ></a-switch>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item
            :label="t('父级菜单')"
            v-bind="validateInfos.parentMenuId"
          >
            <popup-menu v-model:value="modelRef.parentMenuId"></popup-menu>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item
            :label="t('国际化Key值')"
            v-bind="validateInfos.localeKey"
          >
            <a-input
              v-model:value="modelRef.localeKey"
              :placeholder="t('请输入国际化名称')"
              allow-clear
            ></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item
            :label="t('菜单描述')"
            :label-col="{ span: 4 }"
            :wrapper-col="{ span: 20 }"
          >
            <a-textarea
              v-model:value="modelRef.menuDesc"
              :placeholder="t('请输入菜单描述')"
              :rows="2"
              allow-clear
            ></a-textarea>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </dys-modal>
</template>
<script lang="ts" setup>
import { pick } from "lodash";
import { useI18n } from "vue-i18n";
import { reactive, ref, watch } from "vue";
import { Form, message } from "ant-design-vue";
import { add, update } from "@/api/system/menu";
import type { FormRule } from "@/typing";
import type { MenuParams, QueryResult } from "@/api/system/menu/typing";
import PopupMenu from "@/components/popup-menu/index.vue";
import SelectIcon from "@/components/select-icon/index.vue";
interface Props {
  /**弹窗 */
  open: boolean;
  /**行数据 */
  model: QueryResult;
}
const props = defineProps<Props>();
const emit = defineEmits(["cancel", "ok"]);
const { t } = useI18n();
const initValue = (): MenuParams => ({
  menuId: null,
  menuName: null,
  menuCode: null,
  menuIcon: null,
  menuDesc: null,
  menuStatus: "Y",
  parentMenuId: null,
  localeKey: null,
});
const modelRef = reactive(initValue());
const modelRule = reactive<FormRule<MenuParams>>({
  menuName: [{ required: true, message: () => t("请输入菜单名称") }],
  menuCode: [{ required: true, message: () => t("请输入菜单编码") }],
  menuStatus: [{ required: true, message: () => t("请选择菜单状态") }],
  menuIcon: [{ required: true, message: () => t("请选择菜单图标") }],
  localeKey: [{ required: true, message: () => t("请输入国际化名称") }],
});
const { validate, validateInfos, resetFields } = Form.useForm(
  modelRef,
  modelRule
);
watch([() => props.model, () => props.open], ([model]) => {
  resetFields(pick(model, Object.keys(modelRef)) || initValue());
});
const loading = ref(false);
/**提交 */
const handleSubmit = async () => {
  try {
    await validate();
  } catch {
    return message.warning(t("校验失败"));
  }
  loading.value = true;
  const { success } = await (props.model?.menuId ? update : add)(modelRef);
  loading.value = false;
  if (success) {
    message.success(props.model?.menuId ? t("修改成功") : t("新增成功"));
    emit("ok");
    emit("cancel");
  }
};
</script>
